/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.mt.roll.upms.controller;

import com.mt.roll.core.constants.CommonConstant;
import com.mt.roll.core.base.BaseController;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.validator.groups.AddGroup;
import com.mt.roll.core.validator.groups.PageGroup;
import com.mt.roll.core.validator.groups.UpdateGroup;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.upms.params.SysRoleParam;
import com.mt.roll.upms.params.SysRoleQueryParam;
import com.mt.roll.upms.params.UserRoleParam;
import com.mt.roll.upms.service.RoleService;
import com.mt.roll.upms.vo.SysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 平台角色控制器
 *
 * @author jiff
 * @date 2018/11/14
 * @since 1.0
 */
@RestController
@RequestMapping(CommonConstant.API_MODULE_UPMS +"role")
@Slf4j
@Api(value = "平台角色管理", tags = "平台角色管理接口", hidden = false)
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;

    @PostMapping(value = "get")
    @ApiOperation(value = "获取角色")
    private R<SysRoleVo> get(@Validated @RequestBody IdParam idParam) {
        return roleService.get(idParam);
    }

    @PostMapping(value = "page")
    @ApiOperation(value = "获取分页角色列表")
    private R<PageVo<SysRoleVo>> page(@Validated({PageGroup.class, Default.class}) @RequestBody SysRoleQueryParam sysRoleQueryParam) {
        return roleService.page(sysRoleQueryParam);
    }

    @PostMapping(value = "list")
    @ApiOperation(value = "获取角色列表")
    private R<List<SysRoleVo>> list(@Validated({Default.class}) @RequestBody SysRoleQueryParam sysRoleQueryParam) {
        return roleService.list(sysRoleQueryParam);
    }

    @PostMapping(value = "add")
    @ApiOperation(value = "新增角色")
    private R<IdVo> add(@Validated({AddGroup.class, Default.class}) @RequestBody SysRoleParam sysRoleParam) {
        return roleService.add(sysRoleParam);
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新角色")
    private R<IdVo> update(@Validated({UpdateGroup.class, Default.class}) @RequestBody SysRoleParam sysRoleParam) {
        return roleService.update(sysRoleParam);
    }

    @PostMapping(value = "enabled")
    @ApiOperation(value = "启停角色")
    private R<IdVo> enabled(@Validated @RequestBody EnabledParam enabledParam) {
        return roleService.enabled(enabledParam);
    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除角色")
    private R<IdVo> delete(@Validated @RequestBody IdParam idParam) {
        return roleService.delete(idParam);
    }

    @PostMapping(value = "batchDelete")
    @ApiOperation(value = "批量删除角色")
    private R<IdListVo> delete(@Validated @RequestBody IdListParam idListParam) {
        return roleService.delete(idListParam);
    }

    @PostMapping(value = "saveUserRole")
    @ApiOperation(value = "保存用户角色")
    private R<IdListVo> saveUserRole(@Validated @RequestBody UserRoleParam userRoleParam) {
        return roleService.saveUserRole(userRoleParam);
    }

    @PostMapping(value = "listByUser")
    @ApiOperation(value = "获取用户角色")
    private R<List<SysRoleVo>> listByUser(@Validated @RequestBody IdParam idParam) {
        return roleService.listByUser(idParam);
    }
    @PostMapping(value = "get-code")
    @ApiOperation(value = "获取角色code")
    private R<String> getCode() {
        return successful(CodeGeneratorHelper.getRoleCode());
    }
}
